package com.gitee.shop.controller;

import com.gitee.shop.domain.Item;
import com.gitee.shop.domain.ItemCat;
import com.gitee.shop.service.ItemCatService;
import com.gitee.shop.service.ItemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/item")
public class ItemController {

    //注入service
    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemCatService itemCatService;
    //获取分页大小
    @Value("${pageSize:#{null}}")
    private int pageSize;

    //根据分类显示商品信息
    @RequestMapping("/cat/{cid}/{pageNum}")
    public String cat(@PathVariable int cid, @PathVariable int pageNum, Model model){
        //调用分页方法
        PageInfo<Item> pageInfo = itemService.selectPageByCid(cid,pageNum,pageSize);
        //获取分类信息
        ItemCat itemCat = itemCatService.selectByPrimaryKey(cid);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("itemCat",itemCat);
        return "category";
    }

    //显示商品详情
    @RequestMapping("/{id}")
    public String item(@PathVariable int id, Model model){
        System.out.println(id);
        //根据ID查询
        Item item = itemService.selectByPrimaryKey(id);
        System.out.println(item.getImage());
        //将商品信息放入model
        model.addAttribute("item",item);

        return "item";
    }
}


